package com.baby.work.domain.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Aspect
@Component
public class LoggingAspect {

    /**
     * 定义一个方法,用于声明切入点表达式,一般的,该方法中再不需要添加其他代码
     * 主要是为了重用路径,使用@Pointcut来声明切入点表达式
     * 后面的其他通知直接使用方法名来引用当前的切入点表达式
     */
    @Pointcut("execution(* com.baby.work.domain.aop.*.*(..))")
    public void declareJointPointExpression() {}

//    @Before("declareJointPointExpression()")
//    public void beforeMethod(JoinPoint joinPoint) {
//        String method = joinPoint.getSignature().getName();
//        List<Object> args = Arrays.asList(joinPoint.getArgs());
//        System.out.println("The method " + method + " begin with " + args);
//    }

    @Around("declareJointPointExpression()")
    public void aroundMethod(ProceedingJoinPoint point) {
        String method = point.getSignature().getName();
        List<Object> args = Arrays.asList(point.getArgs());
        Object result = null;
        try {
            System.out.println("The method " + method + " begin with " + args);
            result = point.proceed();
            System.out.println("The method " + method + " end with " + result);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

}
